#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>

#define filename "log.txt"

int main(){
    //fprintf(stdout,"hello normal message\n");
    //fprintf(stdout,"hello normal message\n");
    //fprintf(stdout,"hello normal message\n");
    //fprintf(stdout,"hello normal message\n");

    //fprintf(stderr,"hello error message\n");
    //fprintf(stderr,"hello error message\n");
    //fprintf(stderr,"hello error message\n");
    //fprintf(stderr,"hello error message\n");



    //close(0);
    //close(1);
    //close(2);
    //输出重定向
    //int fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC,0666);
    //追加重定向
    //int fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 0666);
    
    //输入重定向
    int fd = open(filename, O_RDONLY);
    if(fd < 0){
        perror("open");
        return 1;
    }

    dup2(fd,0);

    char inbuffer[1024];
    ssize_t s = read(0, inbuffer, sizeof(inbuffer)-1);
    if(s > 0){
        inbuffer[s]='\0';
        printf("echo: %s\n", inbuffer);
    }
    close(fd);

    //printf("fd : %d\n",fd);
    
   // dup2(fd,1);
   // close(fd);

   // const char *message = "hello linux\n";
   // int cnt=5;
   // while(cnt){
   //     write(1,message,strlen(message));
   //     cnt--;
   // }

   // close(fd);

   // return 0;
}
